package com.ruoyi.web.controller.portal;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.portal.constant.Constant;
import com.ruoyi.portal.vo.AliYunConfigVO;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: cjie
 * @description:
 * @date: 2021/5/9
 */
@RestController
@RequestMapping("/portal/config")
public class AliYunConfigController {
    @Resource
    private ISysConfigService configService;

    /**
     * 上传阿里云oss配置
     * @param aliYunConfigVO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('portal:config:add')")
    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody AliYunConfigVO aliYunConfigVO){
        SysConfig retConfig = configService.selectSysConfigByKey(Constant.CLOUD_STORAGE_CONFIG_KEY);

        if (StringUtils.isNull(retConfig)) {
            retConfig = new SysConfig();
            retConfig.setConfigKey(Constant.CLOUD_STORAGE_CONFIG_KEY);
            retConfig.setConfigValue(JSON.toJSONString(aliYunConfigVO));
            configService.insertConfig(retConfig);
        }else{
            retConfig.setConfigValue(JSON.toJSONString(aliYunConfigVO));
            configService.updateConfig(retConfig);
        }

        return AjaxResult.success();
    }

    /**
     * 获取阿里云oss配置
     */
    @PreAuthorize("@ss.hasPermi('portal:config:query')")
    @GetMapping()
    public AjaxResult getInfo() {
        SysConfig retConfig = configService.selectSysConfigByKey(Constant.CLOUD_STORAGE_CONFIG_KEY);
        if(retConfig != null){
            return AjaxResult.success("操作成功",JSON.parseObject(retConfig.getConfigValue(), AliYunConfigVO.class));
        }else{
            return AjaxResult.success();
        }

    }
}
